define(['jquery', 'bootstrap', 'frontend', 'form', 'template','table','backend'], function ($, undefined, Frontend, Form, Template,Table,Backend,undefined) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        login: function () {
            //本地验证未通过时提示
            $("#login-form").data("validator-options", validatoroptions);

            $(document).on("change", "input[name=type]", function () {
                var type = $(this).val();
                $("div.form-group[data-type]").addClass("hide");
                $("div.form-group[data-type='" + type + "']").removeClass("hide");
                $('#resetpwd-form').validator("setField", {
                    captcha: "required;length(4);integer[+];remote(" + $(this).data("check-url") + ", event=resetpwd, " + type + ":#" + type + ")",
                });
                $(".btn-captcha").data("url", $(this).data("send-url")).data("type", type);
            });

            //为表单绑定事件
            Form.api.bindevent($("#login-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });

            Form.api.bindevent($("#resetpwd-form"), function (data) {
                Layer.closeAll();
            });

            $(document).on("click", ".btn-forgot", function () {
                var id = "resetpwdtpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: __('Reset password'),
                    area: ["450px", "355px"],
                    content: content,
                    success: function (layero) {
                        Form.api.bindevent($("#resetpwd-form", layero), function (data) {
                            Layer.closeAll();
                        });
                    }
                });
            });
        },
        register: function () {
            //本地验证未通过时提示
            $("#register-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#register-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
        },
        changepwd: function () {
            //本地验证未通过时提示
            $("#changepwd-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#changepwd-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
        },
        profile: function () {
            // 给上传按钮添加上传成功事件
            $("#plupload-avatar").data("upload-success", function (data) {
                var url = Fast.api.cdnurl(data.url);
                $(".profile-user-img").prop("src", url);
                Toastr.success(__('Upload successful'));
            });
            Form.api.bindevent($("#profile-form"));
            $(document).on("click", ".btn-change", function () {
                var that = this;
                var id = $(this).data("type") + "tpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "修改",
                    area: ["400px", "250px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        Form.api.bindevent(form, function (data) {
                            location.reload();
                            Layer.closeAll();
                        });
                    }
                });
            });
        },
        client:function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: '/index/user/client',
                        table: 'apply_bet',
                    }
                });

                var table = $("#table");

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'apply_bet.create_time',
                    columns: [
                        [
                            {field: 'create_time', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},
                            // {field: 'id', title: '订单ID', sortable: true},
                            {field: 'apply.username', title: '姓名', operate: 'LIKE'},
                            {field: 'apply.tel', title: '号码', operate: 'LIKE'},
                            {field: 'apply.qq', title: 'QQ', operate: 'LIKE'},
                            {field: 'apply.weixin', title: '微信'},
                            {field: 'apply.zhimafen', title: '芝麻分'},
                            {field: 'score', title: '购买积分'},
                            {
                                field: 'feedback',
                                title: '问题反馈',
                                operate:false,
                                formatter:function (value,row) {
                                    var list = ['暂不反馈','电话拒接','电话空号或停机','客户不需要','客户有意向','其他'];

                                    var option = '';
                                    var disabled = value != 0 ? 'disabled ' : $(this).off('change');

                                    list.forEach(function (element,index) {
                                        var selected = value == index ? 'selected':'';
                                        option += "<option "+ selected +" value="+index+" data-id="+row.id+">" + element +"</option>"
                                    })

                                    return "<select "+ disabled +" class=\"form-control selectpicker feedback \">"+
                                        option +
                                        "</select>";
                                }
                            },
                            {
                                field: 'status',
                                title: '订单状态',
                                operate:false,
                                formatter:function (value,row) {
                                    var list = ['未完成','完成'];
                                    var option = '';
                                    var disabled = value != 0 ? 'disabled ' : '';

                                    list.forEach(function (element,index) {
                                        var selected = value == index ? 'selected':'';
                                        option += "<option "+ selected +" value="+index+" data-id="+row.id+">" + element +"</option>"
                                    })

                                    return "<select "+ disabled +" class=\"form-control selectpicker status2 \" \">"+
                                        option +
                                        "</select>";
                                }
                            },
                            // {field: 'status', title: __('Status'),formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                            {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                                buttons:[
                                    // {name:'add_score',title:'积分管理',icon:'fa fa-usd',classname: 'btn btn-xs btn-primary btn-dialog',url:'user/user/score'},
                                    {
                                        name:'del_score',title:'认领',icon:'fa fa-handshake-o',classname: 'btn btn-xs btn-success btn-magic btn-ajax',url:'/index/user/operate',
                                        success: function (data, ret) {
                                            if ( ret.code == 1){
                                                $("#table").bootstrapTable('refresh');
                                            }else{

                                            }
                                        }
                                    }
                                ],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格绑定事件
                // Controller.api.bindevent();
                Table.api.bindevent(table);

            $(document).on('change','.feedback',function () {
                var _val = $(this).val();
                var _id = $(this).children().eq(_val).data('id');

                $.post("/index/user/feedback",{ids:_id,status:_val,type:2},function (ret) {
                    if (ret.code == 1){
                        layer.msg(ret.msg)
                        location.reload();
                    }else{
                        layer.msg(ret.msg)
                    }
                },'json')
            })
            $(document).on('change','.status2',function () {
                var _val = $(this).val();
                var _id = $(this).children().eq(_val).data('id');

                $.post("/index/user/feedback",{ids:_id,status:_val,type:1},function (ret) {
                    if (ret.code == 1){
                        layer.msg(ret.msg)
                        location.reload();
                    }else{
                        layer.msg(ret.msg)
                    }
                },'json')
            })

            $.post('/index/user/order',{},function (ret) {});
        },
        myclient:function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: '/index/user/myclient',
                    table: 'apply_bet'
                },
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'apply_bet.status',
                sortOrder: 'asc',
                columns: [
                    [
                        {field: 'id', title: '订单ID', sortable: true},
                        {field: 'create_time', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},
                        {field: 'apply.username', title: '姓名', operate: 'LIKE'},
                        {field: 'apply.tel', title: '号码', operate: 'LIKE'},
                        {field: 'apply.qq', title: 'QQ', operate: 'LIKE'},
                        {field: 'apply.weixin', title: '微信'},
                        {field: 'apply.zhimafen', title: '芝麻分'},
                        {field: 'score', title: '购买积分'},
                        {
                            field: 'feedback',
                            title: '问题反馈',
                            operate:false,
                            formatter:function (value,row) {
                                var list = ['暂不反馈','电话拒接','电话空号或停机','客户不需要','客户有意向','其他'];

                                var option = '';
                                var disabled = value != 0 ? 'disabled ' : $(this).off('change');

                                list.forEach(function (element,index) {
                                    var selected = value == index ? 'selected':'';
                                    option += "<option "+ selected +" value="+index+" data-id="+row.id+">" + element +"</option>"
                                })

                                return "<select "+ disabled +" class=\"form-control selectpicker feedback \">"+
                                        option +
                                        "</select>";
                            }
                        },
                        {
                            field: 'status',
                            title: '订单状态',
                            operate:false,
                            formatter:function (value,row) {
                                var list = ['未完成','完成'];
                                var option = '';
                                var disabled = value != 0 ? 'disabled ' : '';

                                list.forEach(function (element,index) {
                                    var selected = value == index ? 'selected':'';
                                    option += "<option "+ selected +" value="+index+" data-id="+row.id+">" + element +"</option>"
                                })

                                return "<select "+ disabled +" class=\"form-control selectpicker status2 \" \">"+
                                        option +
                                        "</select>";
                            }
                        },
                    ]
                ]
            });

            $(document).on('change','.feedback',function () {
                var _val = $(this).val();
                var _id = $(this).children().eq(_val).data('id');

                $.post("/index/user/feedback",{ids:_id,status:_val,type:2},function (ret) {
                    if (ret.code == 1){
                        layer.msg(ret.msg)
                        location.reload();
                    }else{
                        layer.msg(ret.msg)
                    }
                },'json')
            })
            $(document).on('change','.status2',function () {
                var _val = $(this).val();
                var _id = $(this).children().eq(_val).data('id');

                $.post("/index/user/feedback",{ids:_id,status:_val,type:1},function (ret) {
                    if (ret.code == 1){
                        layer.msg(ret.msg)
                        location.reload();
                    }else{
                        layer.msg(ret.msg)
                    }
                },'json')
            })

            // 为表格绑定事件
            // Controller.api.bindevent();
            Table.api.bindevent(table);
        },
        score:function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: '/index/user/score',
                    table: 'score_detail',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        {field: 'create_time', title: '创建时间',operate:false, formatter: Table.api.formatter.datetime, addclass: 'datetimerange'},
                        {field: 'residue', title: '剩余积分', operate: 'LIKE',operate:false},
                        {field: 'type', title: '类型', operate: 'LIKE',operate:false,formatter:function (value,row) {
                            console.log(value);
                                switch (value){
                                    case 1:
                                        return '消费';
                                    break;
                                    case 2:
                                        return '充值';
                                    break;
                                    case 3:
                                        return '扣除';
                                    break;
                                }
                            }
                        },
                       {field: 'score', title: '积分',operate:false}
                    ]
                ]
            });

            // 为表格绑定事件
            // Controller.api.bindevent();
            Table.api.bindevent(table);
        }


    };
    return Controller;
});